{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1ede788b",
   "metadata": {},
   "source": [
    "1.导入rclpy和Node"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2b0240ea",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "import rclpy\n",
    "from rclpy.node import Node"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "de0410de",
   "metadata": {},
   "source": [
    "2.从tf2_ros包中导入坐标变换监听器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b3e88382",
   "metadata": {},
   "outputs": [],
   "source": [
    "from tf2_ros import TransformException\n",
    "from tf2_ros.buffer import Buffer\n",
    "from tf2_ros.transform_listener import TransformListener"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a398b6ba",
   "metadata": {},
   "source": [
    "3.初始化节点"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c537ef08",
   "metadata": {},
   "outputs": [],
   "source": [
    "rclpy.init()\n",
    "node = Node(\"transform_node3\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2a56873b",
   "metadata": {},
   "source": [
    "4.初始化节点"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "3242a896",
   "metadata": {},
   "outputs": [],
   "source": [
    "tf_buffer = Buffer()\n",
    "tf_listener = TransformListener(tf_buffer,node)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "b774b42b",
   "metadata": {},
   "outputs": [],
   "source": [
    "def transform_callback():    \n",
    "    try:\n",
    "        now = rclpy.time.Time()\n",
    "        trans = tf_buffer.lookup_transform('B','P',now)\n",
    "        print(\"trans\",trans)\n",
    "    except TransformException as ex:\n",
    "        print(f'Could not transform B to P: {ex}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "523716bc",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Could not transform B to P: \"P\" passed to lookupTransform argument source_frame does not exist. \n",
      "trans\n",
      "trans\n",
      "trans\n",
      "trans\n",
      "trans\n",
      "trans\n",
      "trans\n",
      "trans\n"
     ]
    }
   ],
   "source": [
    "node.create_timer(1,transform_callback)\n",
    "rclpy.spin(node)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ddfe5baa",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "celltoolbar": "原始单元格格式",
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
